home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / sound / rsynth22.zip / DICT.C < prev    next >
Text File  |  1996-07-16  |  2KB  |  134 lines

  1. #include <config.h>
  2. #include "proto.h"
  3. #include <io.h>
  4. #include <stdio.h>
  5. #include "phones.h"
  6.  
  7. void *dict;
  8. char **dialect = ph_am;
  9.  
  10. #ifdef HAVE_LIBGDBM
  11. #include <useconfig.h>
  12. #include <ctype.h>
  13. #include <gdbm.h>
  14.  
  15. #include "dict.h"
  16. #include "getargs.h"
  17.  
  18. #ifndef DICT_DIR
  19. #define DICT_DIR "."
  20. #endif
  21.  
  22. char *dict_path = "a";
  23.  
  24.  
  25. unsigned char *
  26. dict_find(s, n)
  27. char *s;
  28. unsigned n;
  29. {
  30.  if (!n)
  31.   n = strlen(s);
  32.  if (dict)
  33.   {
  34.    datum key;
  35.    datum data;
  36.    int i;
  37.    key.dptr = malloc(n);
  38.    key.dsize = n;
  39.    for (i = 0; i < n; i++)
  40.     {
  41.      key.dptr[i] = (islower(s[i])) ? toupper(s[i]) : s[i];
  42.     }
  43.    data = gdbm_fetch((GDBM_FILE) dict, key);
  44.    free(key.dptr);
  45.    if (data.dptr)
  46.     {
  47.      unsigned char *w = (unsigned char *) malloc(data.dsize + 1);
  48.      memcpy(w, data.dptr, data.dsize);
  49.      w[data.dsize] = 0;
  50.      free(data.dptr);
  51.      return w;
  52.     }
  53.   }
  54.  return NULL;
  55. }
  56.  
  57. static void choose_dialect PROTO((void));
  58.  
  59. static void
  60. choose_dialect()
  61. {
  62.  unsigned char *word = dict_find("schedule", 0);
  63.  if (word)
  64.   {
  65.    if (word[0] == SH)
  66.     dialect = ph_br;
  67.    else if (word[0] == S && word[1] == K)
  68.     dialect = ph_am;
  69.    free(word);
  70.   }
  71. }
  72.  
  73. int
  74. dict_init(argc, argv)
  75. int argc;
  76. char *argv[];
  77. {
  78.  char *buf = NULL;
  79.  char *dictdir;
  80.  argc = getargs("Dictionary", argc, argv, "d", "", &dict_path, "Which dictionary [b|a]", NULL);
  81.  dictdir = getenv("ETC");
  82.  if (!help_only)
  83.   {
  84.    buf = malloc(strlen(dictdir) + strlen("Dict.db") + strlen(dict_path) + 2);
  85.    sprintf(buf, "%sDict.db", dict_path);
  86.    if (!(dict = gdbm_open(buf, GDBM_READER, 0, 0666, NULL)))
  87.     {                
  88.      sprintf(buf, "%s/%sDict.db", dictdir, dict_path);
  89.      dict = gdbm_open(buf, GDBM_READER, 0, 0666, NULL);
  90.     }                
  91.  
  92.   if (dict)         
  93.     {                
  94.      dict_path = realloc(buf, strlen(buf) + 1);
  95.     }                
  96.    if (dict)         
  97.     choose_dialect();
  98.   }
  99.  return argc;
  100. }
  101.  
  102. void
  103. dict_term()
  104. {
  105.  if (dict)
  106.   gdbm_close((GDBM_FILE) dict);
  107. }
  108.  
  109. #else
  110.  
  111. unsigned char *
  112. dict_find(s, n)
  113. char *s;
  114. unsigned n;
  115. {
  116.  return NULL;
  117. }
  118.  
  119. int
  120. dict_init(argc, argv)
  121. int argc;
  122. char *argv[];
  123. {
  124.  return argc;
  125. }
  126.  
  127. void
  128. dict_term()
  129. {
  130.  
  131. }
  132.  
  133. #endif
  134.